package com.huabo.system.service.impl;

import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblAttachment;
import com.hbfk.entity.TblStaffUtil;
import com.hbfk.util.FtpUtil;
import com.hbfk.util.JsonBean;
import com.hbfk.util.ResponseFormat;
import com.huabo.system.config.DateBaseConfig;
import com.huabo.system.mysql.mapper.TblAttachmentMySqlMapper;
import com.huabo.system.oracle.mapper.TblAttachmentMapper;
import com.huabo.system.service.TblAttachmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TblAttachmentServiceImpl implements TblAttachmentService {

    @Resource
    private TblAttachmentMapper tblAttachmentMapper;

    @Resource
    private TblAttachmentMySqlMapper tblAttachmentMySqlMapper;

    @Override
    public TblAttachment selectAtt(BigDecimal attid) {


        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {

            return tblAttachmentMapper.selectAtt(attid);
        } else {

            return tblAttachmentMySqlMapper.selectAtt(attid);
        }

    }

    @Override
    public List<TblAttachment> findAllByTblNBSJSheet(String id) {
        if (DateBaseConfig.DATABASETYPE.equals("Oracle")) {
            return tblAttachmentMapper.findAllByTblNBSJSheet(id);
        } else {

            return tblAttachmentMySqlMapper.findAllByTblNBSJSheet(id);
        }


    }

	@Override
	public JsonBean getPrivewAttInfo(String token, Integer attId, Integer attType) throws Exception {
		TblStaffUtil loginStaff = DealUserToken.parseUserToken(token);
		if (loginStaff == null) {
			return ResponseFormat.retParam(0, 20006, null);
		}
		Map<String, Object> resultMap = new HashMap<String, Object>(0);
		String ftpUrl = FtpUtil.Ftpip;
		String fileName = null;
		switch (attType) {
		case 1:
			ftpUrl += "/constract";
			fileName = this.tblAttachmentMapper.selectSignFileNameById(attId);
			break;
		default:
			ftpUrl += "/uploadfile";
			fileName = this.tblAttachmentMapper.selectUploadFileNameById(attId);
			break;
		}
		if(fileName == null || "".equals(fileName)) {
			return ResponseFormat.retParam(0, 50006, null);
		}
		ftpUrl += "/"+fileName;	
		
		resultMap.put("ftpUrl", "ftp://"+ftpUrl);
		resultMap.put("previewurl", FtpUtil.previewurl);
		resultMap.put("param","url");
		return ResponseFormat.retParam(1, 200, resultMap);
	}
}
